Skip to content

feat: 新增 Provider 抽象系统支持插件提供翻译和 OCR 能力#560

Merged
lzx8589561 merged 3 commits into
ZToolsCenter:mainfrom
Particaly:feature/third-part-function-provider
Jun 29, 2026
Merged

feat: 新增 Provider 抽象系统支持插件提供翻译和 OCR 能力#560
lzx8589561 merged 3 commits into
ZToolsCenter:mainfrom
Particaly:feature/third-part-function-provider

Conversation

@Particaly

Copy link
Copy Markdown
Collaborator

变更概要:

  • 新增 providerManager 核心模块,统一管理翻译、OCR 等能力提供商
  • 插件可通过 plugin.json 的 providers 字段声明提供的能力类型
  • 内置 Bergamot 翻译引擎注册为默认 translation provider
  • 新增设置页面 ProvidersSetting,支持查看和切换不同 provider
  • 新增插件 API:ztools.registerProvider() 和 ztools.callProvider()
  • 新增完整的类型定义 providerShared.ts 和单元测试
  • 提供插件开发示例和开发指南文档

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new 'Provider' abstraction to ZTools, allowing translation and OCR capabilities to be provided by plugins and managed centrally. It includes a new 'Providers' settings page, a ProviderManager to aggregate built-in and plugin providers, and exposed APIs for other plugins to consume these services. Feedback on the changes suggests correcting a documentation typo regarding translation.image, adding error logging to a silent catch block in the settings UI, and replacing an any type with a concrete type in the TypeScript definitions for better type safety.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

| `translation` | 文本翻译 | `{ text, from?, to? }` | `{ text, detectedFrom? }` |
| `ocr` | 图片文字识别 | `{ image, lang? }` | `{ text, blocks?, confidence? }` |

- `translation.image` / `ocr.image` 可为:本地路径 / `data:` URI / `http(s)` URL(具体支持取决于实现)。

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

文档此处似乎有一个小错误。根据表格中的定义,translation 提供商的输入是文本,不包含 image 属性。image 属性是 ocr 提供商的输入。建议修正此处的描述以避免开发者混淆。

Suggested change
- `translation.image` / `ocr.image` 可为:本地路径 / `data:` URI / `http(s)` URL(具体支持取决于实现)。
- `ocr.image` 可为:本地路径 / `data:` URI / `http(s)` URL(具体支持取决于实现)。

Comment on lines +41 to +43
} catch {
providerTypes.value = []
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

catch 块中静默地处理错误可能会导致问题难以追踪。建议使用 console.error 记录错误信息,这样在出现问题时可以更方便地进行调试。

  } catch (err) {
    console.error(`加载插件 [${props.plugin.name}] 的 provider 类型失败:`, err)
    providerTypes.value = []
  }

setParams: (
providerId: string,
params: Record<string, unknown>
) => Promise<{ success: boolean; data?: any; error?: string }>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

为了增强类型安全和代码可读性,建议为 setParams 方法返回的 data 提供一个具体的类型,而不是使用 any。可以参考 getSettings 等方法的返回类型,保持一致性。

Suggested change
) => Promise<{ success: boolean; data?: any; error?: string }>
) => Promise<{ success: boolean; data?: { enabled: Partial<Record<'translation' | 'ocr', string[]>>; defaultId: Partial<Record<'translation' | 'ocr', string>>; params: Record<string, Record<string, unknown>>; }; error?: string }>

@Particaly

Copy link
Copy Markdown
Collaborator Author

部分功能还不够稳定,再等我看看迭代一下

将 providers 字段的 key 从「必须等于 type」改为「插件内唯一标识」,
value.type 决定类别。运行时 registerProvider / __invokeRegisteredProvider
/ 注册跟踪全部改为按声明 key 派发,使单插件可对同一 type 声明多条
(如 baidu/google 都为 translation)。

向后兼容:旧插件 key===type 用法保持不变。

- src/shared/providerShared.ts: PluginProvidersField 改为 Record<string, decl>,
  ProviderEntry 新增 key 字段,buildPluginProviderId 参数语义改为 key
- src/main/core/provider/providerManager.ts: sanitize/getAllProviders/
  registerProvider/invoke 全部按 key 工作
- resources/preload.js: registerProvider/__invokeRegisteredProvider 按 key 存取
- docs: 更新开发指南与示例 README,补「同一 type 多条」示例
- tests: 新增多声明用例,保留兼容回归
@lzx8589561 lzx8589561 merged commit f0cb51f into ZToolsCenter:main Jun 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants